//
//  SingerListController.m
//  LZMusicPlayer
//
//  Created by wulanzhou on 2016/11/8.
//  Copyright © 2016年 wulanzhou. All rights reserved.
//

#import "SingerListController.h"
#import "ArMenuScrollView.h"
#import  <SDWebImage/UIImageView+WebCache.h>
#import "AlbumArtBLL.h"

@interface SingerListController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *singerTable;
@property (nonatomic,strong) AlbumArtBLL *artBLL;
@property (nonatomic,strong) ArMenuScrollView *areaMenuScrollView;
@property (nonatomic,strong) ArMenuScrollView *sexMenuScrollView;

@end

@implementation SingerListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"歌手";
    [self.view addSubview:self.singerTable];
    
    [self.artBLL loadHotArt:^{
        [self.singerTable reloadData];
    }];
    // Do any additional setup after loading the view.
}

- (void)searchArt{

    //地区：0不分,6华语,3欧美,7韩国,60日本,5其他
    NSInteger area=0;
    if (self.areaMenuScrollView.selectedIndex == 1) {
        area = 6;
    }else if (self.areaMenuScrollView.selectedIndex == 2){
        area = 7;
    }else if (self.areaMenuScrollView.selectedIndex == 3){
        area = 60;
    }else if (self.areaMenuScrollView.selectedIndex == 4){
        area = 3;
    }
    
    [self.artBLL searchArtWithArea:area sex:self.sexMenuScrollView.selectedIndex completion:^{
        [self.singerTable reloadData];
    }];
}

#pragma mark -懒加载

- (AlbumArtBLL *)artBLL{

    if (!_artBLL) {
        _artBLL = [[AlbumArtBLL alloc] init];
    }
    return _artBLL;
}

- (UIView *)tableHeaderView{
    
    UIView *headerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 0)];

    ArMenuScrollView *arMenu = [[ArMenuScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 44) titles:[NSArray arrayWithObjects:@"全部",@"华语",@"韩国",@"日本",@"欧美", nil]];
    [arMenu setSelectedItemCompletion:^(NSInteger index, NSString *title) {
        [self searchArt];
    }];
    [headerView addSubview:arMenu];
    self.areaMenuScrollView = arMenu;
    UILabel *labLine = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(arMenu.frame), self.view.width, 0.5)];
    labLine.backgroundColor = UIColorMakeRGB(222, 222, 222);
    [headerView addSubview:labLine];
    
    ArMenuScrollView *arSexMenu = [[ArMenuScrollView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(labLine.frame), self.view.width, 44) titles:[NSArray arrayWithObjects:@"全部",@"男",@"女",@"组合", nil]];
    [arSexMenu setSelectedItemCompletion:^(NSInteger index, NSString *title) {
        [self searchArt];
    }];
    [headerView addSubview:arSexMenu];
    self.sexMenuScrollView = arSexMenu;
    labLine = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(arSexMenu.frame), self.view.width, 0.5)];
    labLine.backgroundColor = UIColorMakeRGB(222, 222, 222);
    [headerView addSubview:labLine];
    
    CGRect r=headerView.frame;
    r.size.height=CGRectGetMaxY(labLine.frame);
    headerView.frame=r;
    
    return headerView;
}

- (UITableView *)singerTable{
    if (!_singerTable) {
        _singerTable = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _singerTable.tableHeaderView=[self tableHeaderView];
        _singerTable.dataSource = self;
        _singerTable.delegate = self;
    }
    return _singerTable;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table Source & Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.artBLL.artSortKey count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    NSString *key = self.artBLL.artSortKey[section];
    return [[self.artBLL.allArtList objectForKey:key] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *cellIdentifier = @"artcellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    NSString *key = self.artBLL.artSortKey[indexPath.section];
    NSArray *source = [self.artBLL.allArtList objectForKey:key];
    
    AlbumArt *item = source[indexPath.row];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:item.avatar_mini] placeholderImage:nil];
    cell.textLabel.text = item.name;
    return cell;
}

#pragma mark - Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - index
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
   return 23.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *sectionBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScWidth, 23)];
    sectionBackView.backgroundColor = RGB(242, 242, 242);
    UILabel *sectionLable = [[UILabel alloc] initWithFrame:CGRectMake(20, 1.5, ScWidth-20, 20)];
    sectionLable.font = [UIFont systemFontOfSize:13.0];
    sectionLable.text = [self.artBLL.artSortKey objectAtIndex:section];
    sectionLable.backgroundColor=[UIColor clearColor];
    [sectionBackView addSubview:sectionLable];
    return sectionBackView;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.artBLL.artSortKey;
}
@end
